#coding=utf-8
'''
Created on 2015年3月16日
@author: wangpeng
'''
#导入相关模块
import urllib
import httplib
import json
import os
import os.path
import urlparse
import time
import random
#导入logging保存和输出文件模板 import logging_save
import logging_save
import re
import serial
import logging
#暂时只支持单曲循环播放，需要结合monkey runner配合切换到相应模式，比如一个列表测试


#基本思路： 截取某个专辑目录里面所有歌曲，但是验证是否是//顺序播放/随机播放列表循环/单曲循环/
#单曲循环思路：选取一首歌曲反复播放即可
#循序播放：一个专辑一次确认
#随机播放： 验证是否循环播放
#选择歌曲：我是歌手第十一期 手法歌手坚守站
testurl="http://image.kaolafm.net/mz/audios/201408/e6191927-1509-4cbd-b90b-0c741373ee31.mp3?appid=marxc4577&deviceid=100000029165298&audioid=1000000326510"
class playmode(object):
    #构造函数定义，确定连接基本信息
    def __init__(self,conn=httplib.HTTPConnection('192.168.1.101'),headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/html"}):
        self.Connection=conn  
        self.Headers=headers
    #单曲播放，设置play——mode为3，然后直接跳转歌曲到快结束，测试10次，之后对比关键值是否一致
    #软件有一个小Bug：可能几首歌不是单曲循环播放，一直往下切换，切换10次之后又重新切换回最先开始歌曲
    def one_repeat(self):
        logging_save.logging.info("现在开始单曲循环播放测试，已经设置为单曲循环播放状态") 
        before_url=getinfo_url(self)
        time.sleep(3)  
        i=0
        post_repeat={
                "command":"play-mode-set",
                "play_mode":3
                 }
        jsonpost_repeat=json.dumps(post_repeat)
        self.Connection.request('POST', '/api/play-mode-set',jsonpost_repeat,self.Headers)
        self.Connection.close()
        gototime=getinfo_totaltime(self)-5
        logging_save.logging.info("单曲循环播放5次")
        while i<10:
            seek_time(modetest,gototime)
            time.sleep(10)
        #self.Connection.close()
            i=i+1
        after_url=getinfo_url(self)
        if before_url==after_url:
            logging_save.logging.info("单曲循环播放测试成功，请耐心等待")
        else:
            logging_save.logging.warning('FAIL,FAIL,FAIL,单曲循环播放失败')
    '''
    #无法获取当前列表，无法测试
    #播放本地歌曲，循序播放        
    def flow_test(self):
        local_postbody={"command":"pb-start-url",
                  "value":'http://192.168.0.152:14280/mnt/sdcard/kgmusic/download/12.mp3', 
                  "title":"", 
                  "artist":"",
                  "album": "", 
                  "mtype":5
        }
        PlaybackURLJson=json.dumps(local_postbody)
        self.Connection.request('POST','/api/pb-start-url',PlaybackURLJson,self.Headers)
        self.Connection.close()
        logging_save.logging.info('此时播放本地电脑歌曲。。。。') 
        i=0
        #循序播放
        post_flow={
                "command":"play-mode-set",
                "play_mode":0
                 }
        jsonpost_flow=json.dumps(post_flow)
        self.Connection.request('POST', '/api/play-mode-set',jsonpost_flow,self.Headers)
        self.Connection.close()
        
        gototime=getinfo_totaltime(self)-30
        while i<10:
            seek_time(modetest,gototime)
            time.sleep(15)
            next_music(self)
        #self.Connection.close()
            i=i+1
#但是怎么推送app本地歌曲
            time.sleep(5)
            #detect_status(self)
        
    '''    
#获取当前播放歌曲总时间
def getinfo_totaltime(self):
        self.Connection.request('GET', '/api/request-pb')
        getinformation=self.Connection.getresponse()
        readinformation=getinformation.read()
        #print readinformation
        getdeviceinfo=eval(readinformation)
        currentvolume=getdeviceinfo['total_time']
        return currentvolume
        self.Connection.close()
        
#获取当前播放歌曲总时间
def getinfo_url(self):
        self.Connection.request('GET', '/api/request-pb')
        getinformation=self.Connection.getresponse()
        readinformation=getinformation.read()
        #print readinformation
        getdeviceinfo=eval(readinformation)
        currentvolume=getdeviceinfo['url']
        return currentvolume
        self.Connection.close()   

       
#播放具体的url播放        
def set_playurl(self,playurl):
    postbody_playurl={
                      "command":"pb-start-url",
                      "value":playurl,
                      "artist":"",
                      "album": "", 
                       "mtype":0
                      }
    #postbody_setvolume={"command":"vol-setting","value":setvolume}
    postjson=json.dumps(postbody_playurl)
    self.Connection.request('POST', '/api/pb-start-url',postjson,self.Headers)
    self.Connection.close()    
    #logging_save.logging.info("TESTING....")

def seek_time(self,testtime):
    postbody_seek_time={
                      "command":"seek-time",
                      "time":testtime
                      }
    #postbody_setvolume={"command":"vol-setting","value":setvolume}
    postjson=json.dumps(postbody_seek_time)
    self.Connection.request('POST', '/api/seek-time',postjson,self.Headers)
    self.Connection.close()  
#设置单曲循环播放，基本思路是播放一首歌曲，然后seek time不同时间跳到歌曲快要结束时间点

#检测状态值    
def detect_status(self):
    self.Connection.request('GET', '/api/play-mode-query')
    getinformation=self.Connection.getresponse()
    readinformation=getinformation.read()
    print readinformation

#切换到下一首歌曲
def next_music(self):
        print '切换到下一首曲目，请等待。。。'
        postbody_next='{"command":"key","value":7}'  
        self.Connection.request('POST', '/api/key',postbody_next,self.Headers)
        self.Connection.close()
        


#if __name__=="__main__":

modetest=playmode() 
modetest.one_repeat()
#modetest.flow_test()

